import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:yysk_app/model/common_model/get_certification_type_model.dart';
import 'package:yysk_app/model/pay_model/open_wallet_info_model.dart';
import 'package:yysk_app/model/pay_model/user_register_apply_model.dart';
import 'package:yysk_app/net/api/common_api.dart';
import 'package:yysk_app/net/api/pay_url.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/net/response_data.dart';
import 'package:yysk_app/pages/nft_pages/wallet_pages/enterprise_auth_zhifu/my_webview_zhifu_open_page.dart';
import 'package:yysk_app/pages/nft_pages/wallet_pages/open_wallet_fail/open_wallet_fail_page.dart';
import 'package:yysk_app/pages/nft_pages/wallet_pages/wallet/wallet_controller.dart';
import 'package:yysk_app/pages/nft_pages/wallet_pages/wallet_personal/wallet_personal_page.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:oktoast/oktoast.dart';

import '../../../my_webview/my_webview_for_app_page.dart';
import '../../../my_webview/my_webview_page.dart';
import '../enterprise_auth_zhifu/my_webview_zhifu_page.dart';

class OpenWalletController extends GetxController {
  var agree = true.obs;
  var loading = false.obs;
  final infoDetail = Rx<GetCertificationTypeModel?>(null);

  final openWalletInfo = Rx<OpenWalletInfoModel?>(
    OpenWalletInfoModel(
      bankAccount: BankAccount(
        accountType: "",
        bankCode: "",
        bankName: "",
        branch: "",
        cardNo: "",
        city: "",
        openingLicenseUrl: "",
        province: "",
      ),
      businessAddressInfo: BusinessAddressInfo(
        address: "",
        city: "",
        district: "",
        province: "",
      ),
      channel: "HUIFU",
      legalPersonInfo: LegalPersonInfo(
        legalLicenceBacktUrl: "",
        legalLicenceFrontUrl: "",
        legalPersonLicenceNo: "",
        legalPersonMobile: "",
        legalPersonName: "",
      ),
      plat: "USER",
      subjectInfo: SubjectInfo(
        certNo: "",
        certUrl: "",
        certValidate: "",
        contactMobile: "",
        signName: "",
      ),
    ),
  );
  var certBeginDate = ''.obs;
  var certEndDate = ''.obs;
  var isLong = false.obs;
  String channel = '';
  @override
  void onInit() {
    super.onInit();
    channel =
        (Get.arguments['channel'] ?? '') == 'DEBIT_CARD' ? 'HUIFU' : 'DIN_PAY';
    // 初始化默认值
    openWalletInfo.value?.channel = channel;
    // 初始化默认值
    openWalletInfo.value?.plat = 'USER';

    updateUserInfo();
  }

  @override
  void onClose() {
    super.onClose();
  }

  Future<void> submit() async {
    if (agree.value == false) {
      showToast('请同意并阅读免责声明');
      return;
    }
    if (certBeginDate.value == '' || certEndDate.value == '') {
      showToast('请选择证件有效期');
      return;
    }
    http.netFetch<UserRegisterApplyModel>(
      PayUrl.registerApply,
      method: Method.post,
      params: {
        'channel': channel,
        'plat': "USER",
        'legalPersonInfo': {
          'certValidityType': isLong.value == true ? 1 : 0,
          'certBeginDate': certBeginDate.value,
          'certEndDate': certEndDate.value,
        },
      },
      success: (r) {
        if (r.data?.applicationStatus == 'REFUSE') {
          Get.toNamed(
            '${OpenWalletFailPage.path}?reason=${r.data?.reason}&channel=${channel}',
            arguments: {'reason': r.data?.reason, 'channel': channel},
          );
        } else if (r.data?.applicationStatus == 'PASS') {
          WalletController walletController = Get.find();
          walletController.getListPay().then((e) {
            Get.back();
          });
        } else if (r.data?.applicationStatus == 'WAITING' &&
            channel == 'DIN_PAY') {
          Get.toNamed(
            '${MyWebViewZhifuOpenPage.path}?url=${Uri.encodeComponent(r.data?.returnUrl ?? '')}',
            arguments: {'url': Uri.encodeComponent(r.data?.returnUrl ?? '')},
          );
        }
        return r;
      },
    );
    return;
  }

  Future<dynamic> updateUserInfo() async {
    final res = http.netFetch<GetCertificationTypeModel>(
      CommonApi.getCertificationType,
      method: Method.post,
      success: (r) {
        infoDetail.value = r.data;
        return r;
      },
    );
    return res;
  }

  void pickValidityDate(int type) async {
    DateTime? picked = await showDatePicker(
      context: Get.context!,
      initialDate: DateTime.now(),
      firstDate: DateTime(1900),
      lastDate: DateTime(2100),
      initialEntryMode: DatePickerEntryMode.input,
    );
    if (picked != null) {
      if (type == 0) {
        certBeginDate.value =
            '${picked.year}${picked.month > 10 ? picked.month : "0${picked.month}"}${picked.day > 10 ? picked.day : "0${picked.day}"}';
      } else {
        certEndDate.value =
            '${picked.year}${picked.month > 10 ? picked.month : "0${picked.month}"}${picked.day > 10 ? picked.day : "0${picked.day}"}';
      }
    }
  }
}
